package org.xg.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xg.domain.UserVO;
import org.xg.service.UserBS;
import org.xg.utils.PageUtils;

import com.googlecode.genericdao.search.ISearch;
import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;

@Controller
public class UserController {
	
	@Resource
	private UserBS userBS;
	
	@RequestMapping(value="/user/index")
	public String index( HttpServletRequest req ) {
		return "/manage/user/index";
	}
	
	@ResponseBody
	@RequestMapping(value="user/search")
	public Map<String,Object> searchUser( HttpServletRequest req, 
			@RequestParam(value="username",defaultValue="")String username, 
			String userCname ) {
		Search search = PageUtils.initPageFromExtGridParam(req);
		search.setResultMode(ISearch.RESULT_MAP);
		search.addField("id");
		search.addField("username");
		search.addField("userCname");
		search.addField("userType");
		search.addField("sex");
		search.addField("email");
		search.addField("phone");
		search.addField("valid");
		SearchResult<UserVO> searchResult = userBS.searchAndCount(search);
		return PageUtils.convertSearchResultToMap(searchResult) ;
	}
	
	@ResponseBody
	@RequestMapping(value="user/saveOrUpdate")
	public Map<String,Object> saveOrUpdate( UserVO user) throws Exception{
		Map<String,Object> m = new HashMap<String,Object>();
		int t = userBS.saveOrUpdate(user);
		m.put("res", t);
		return m ;
	}
	
	@ResponseBody
	@RequestMapping(value="user/getUserByName")
	public Object getUser( String username ) throws Exception{
		UserVO user = userBS.findUserByUserName(username);
		return user ;
	}
	
	@ResponseBody
	@RequestMapping(value="user/getUserById")
	public Object getUserById( String id ) throws Exception{
		UserVO user = userBS.findUserById(id);
		return user ;
	}
	
}

